﻿using System.Collections.Generic;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.Events
{
    internal class EventCategoryChangeRule : EventsRule<EventIndicator>
    {
        public override Severity Severity => Severity.Yellow;
        public override BusinessResearchProduct BusinessResearchProduct => BusinessResearchProduct.Events;
        public override string Code => "YR11";
        public override string InformationalMessage => "Any event changes from or to category 5";

        public EventCategoryChangeRule()
        {
            For(x => x.AnswerCategory).When(Validator).WithKey(x => x.CompanyId)
                .WithMessage("The company {0} #{1}# has event indicator category changed from or to category 5.");
        }

        private bool Validator(EventIndicator validatedEvent, EventIndicator referenceEvent)
        {
            if (validatedEvent.AnswerCategory != 5 && referenceEvent.AnswerCategory != 5)
                return true;

            if (validatedEvent.AnswerCategory == referenceEvent.AnswerCategory)
                return true;

            var triggerEvents = new List<string>() {$"{validatedEvent.ControversyIndicatorNumber}#{validatedEvent.Name}"};

            SetTriggersMessages(triggerEvents);

            return false;
        }
    }
}
